package com.db.bankapp.service.bank;

import com.db.bankapp.domain.bank.Account;
import com.db.bankapp.domain.bank.Bank;
import com.db.bankapp.domain.bank.Client;

/**
 * @ThreadSafe (relies on underlying classes)
 * Provider of bank services
 * Created by Student on 18.08.2014.
 */
public class BankService {

    /**
     * Adds client to bank
     * @param bank to which add client
     * @param client to add to bank
     */
    public static void addClient(Bank bank, Client client) {
        bank.addClient(client);
    }

    /**
     * Return and prints maximum amount to withdraw in bank
     * @param bank in which to search
     * @return maximum amount to withdraw in bank
     */
    public static int printMaximumAmountToWithdraw(Bank bank) {
        int max = 0;
        for(Client client : bank.getClients()) {
            for (Account account : client.getAccounts()) {
                if (account.maximumAmountToWithdraw() > max) {
                    max = account.maximumAmountToWithdraw();
                }
            }
        }
        // todo: uncomment when better times come
        // buzzy ^.^
//        int max = bank.getClients().stream().map(
//                (client) -> client.getAccounts().stream()
//                        .map(Account::maximumAmountToWithdraw).max(Integer::compareTo).get())
//                .max(Integer::compareTo).get();
        System.out.println(max);
        return max;
    }
}
